package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func buildTree() *TreeNode {
	var val int
	fmt.Scan(&val)
	if val == -1 {   // 如果输入的值为-1，则表示当前节点为空
		return nil
	}
	root := &TreeNode{Val: val}
	fmt.Printf("请输入%d的左子树:",val)
	root.Left = buildTree()
	fmt.Printf("请输入%d的右子树:",val)
	root.Right = buildTree()
	return root
}

func preorderTraversal(root *TreeNode) []int {
	if root == nil {
		return []int{}
	}
	res := []int{root.Val}
	res = append(res, preorderTraversal(root.Left)...)
	res = append(res, preorderTraversal(root.Right)...)
	return res
}

func inorderTraversal(root *TreeNode) []int {
	if root == nil {
		return []int{}
	}
	res := inorderTraversal(root.Left)
	res = append(res, root.Val)
	res = append(res, inorderTraversal(root.Right)...)
	return res
}

func postorderTraversal(root *TreeNode) []int {
	if root == nil {
		return []int{}
	}
	res := postorderTraversal(root.Left)
	res = append(res, postorderTraversal(root.Right)...)
	res = append(res, root.Val)
	return res
}

func main() {
	fmt.Println("请输入二叉树的结点值，-1 表示为空：")
	root := buildTree()
	fmt.Println("二叉树的前序遍历结果为：", preorderTraversal(root))
	fmt.Println("二叉树的中序遍历结果为：", inorderTraversal(root))
	fmt.Println("二叉树的后序遍历结果为：", postorderTraversal(root))
}